package com.xtjc03.domain.service.impl;

import com.xtjc03.domain.entity.Beep;
import com.xtjc03.domain.service.BeepService;
import com.xtjc03.serial.service.SerialPortServiceImpl;
import com.xtjc03.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class BeepServiceImpl implements BeepService {

    private static final String COMPANY = "xtjc03";

    @Autowired
    TimeUtil timeUtil;

    @Autowired
    SerialPortServiceImpl serialPortService;

    @Override
    public Beep start() {
        return getComMsg("打开蜂鸣器", "beep0");
    }

    @Override
    public Beep stop() {
        return getComMsg("关闭蜂鸣器", "beep1");
    }

    @Override
    public Beep read() {
        return getComMsg("读取蜂鸣器状态", "beep2");
    }
    
    public Beep getComMsg(String msg, String cmd) {
        Beep beep = new Beep();
        beep.setCmdStatus(msg);
        String s = serialPortService.sendStringToCom("xtjc03beep", COMPANY + cmd + "\r\n");
        if (s != null){
            beep.setDeviceStatus(s.substring(10).startsWith("1") ? "开" : "关");
            beep.setCurrentDate(timeUtil.formatDate(new Date()));
        }
        return beep;
    }
}
